﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Globalization;
using System.Windows.Media;
using Microsoft.Win32;

namespace CAION2.Classes
{
    static class ClassConfiguration
    {
        private const string RegistryPath = "SOFTWARE\\Calculator AION";

        public const byte CharacterMaximumLevel = 65;
        public const int RecipesMaximumLevel = 550;

        public static Color RedColor = Color.FromArgb(153, 218, 2, 2);

        internal static void SetValue(string key, object value)
        {
            RegistryKey currRegistryKey = Registry.LocalMachine.CreateSubKey(RegistryPath);
            if (currRegistryKey == null) return;

            currRegistryKey.SetValue(key, value);
            currRegistryKey.Close();
        }

        internal static object GetValue(string key)
        {
            object value = null;

            RegistryKey currRegistryKey = Registry.LocalMachine.CreateSubKey(RegistryPath);
            
            if (currRegistryKey != null)
            {
                value = currRegistryKey.GetValue(key);
                currRegistryKey.Close();
            }

            if (value == null)
            {
                value = GetDefaultValue(key);
                SetValue(key, value);
            }

            return value;
        }

        private static object GetDefaultValue(string key)
        {
            switch (key)
            {
                case "InterfaceLanguage": return "ru-RU";
                case "ItemsLanguage": return "ru";
                case "ThemeColor": return "Dark";
                case "FontSize": return "Large";
                case "AccentColor": return 0;
                case "CharacterMinLevel": return 1;
                case "CharacterMaxLevel": return CharacterMaximumLevel.ToString(CultureInfo.InvariantCulture);
                case "RecipesMinLevel": return 1;
                case "RecipesMaxLevel": return RecipesMaximumLevel.ToString(CultureInfo.InvariantCulture);
                case "RecipesRaceA": return Boolean.TrueString;
                case "RecipesRaceE": return Boolean.TrueString;
                case "CraftProfession": return 0;
                case "RecipesQualityCommon": return Boolean.TrueString;
                case "RecipesQualityRare": return Boolean.TrueString;
                case "RecipesQualityLegend": return Boolean.TrueString;
                case "RecipesQualityUnique": return Boolean.TrueString;
                case "RecipesQualityEpic": return Boolean.TrueString;
                case "RecipesQualityMythic": return Boolean.TrueString;
                case "SelectItemQualityFilter": return "111111";
                case "StatsListColumnVisible": return "1111";
                case "StigmaRaceA": return Boolean.TrueString;
                //case "MaxRecipesInUse": return 5;
                case "MaxDeepLevel": return 10;
                case "MaxDeepLevelForMulti": return 10;
            }

            return null;
        }

        internal static bool GetBool(string confParam)
        {
            bool result;

            try
            {
                result = Convert.ToBoolean(GetValue(confParam));
            }
            catch (Exception)
            {
                result = false;
            }

            return result;
        }

        internal static Int32 GetInt32(string confParam)
        {
            Int32 result;

            try
            {
                result = Convert.ToInt32(GetValue(confParam));
            }
            catch (Exception)
            {
                result = 0;
            }

            return result;
        }


        internal static ObservableCollection<bool> GetBoolGroup(string confParam)
        {
            ObservableCollection<bool> result = new ObservableCollection<bool>();
            string value = GetValue(confParam).ToString();

            foreach (char c in value)
            {
                result.Add(c == '1');
            }

            return result;
        }

        internal static void SetBoolGroup(string confParam, IEnumerable<bool> values)
        {
            string result = "";

            foreach (bool value in values)
            {
                if (value)
                {
                    result = result + "1";
                }
                else
                {
                    result = result + "0";
                }
            }

            SetValue(confParam, result);
        }
    }
}

